import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/exit_tips.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_tb_base_measures_task_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/hazard_investigation_task_management/hazard__task_mget_controller.dart';

///隐患排查 任务信息管理
class HazardInvestigationTaskManagementPage extends StatelessWidget {
  HazardInvestigationTaskManagementPage({super.key}){
    Get.lazyPut(HazardInvestigationTaskManagementController.new);
  }
  @override
  Widget build(BuildContext context) {
    final HazardInvestigationTaskManagementController controller = Get.find<HazardInvestigationTaskManagementController>();
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          key: controller.rightScaffoldKey,
          body: Stack(
            children: [
              Container(
                color: AppColors.bg_color_grey,
                padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
                child: Column(
                  children: [
                    _topWidget(controller),
                    AppGaps.vGap14,
                    Expanded(
                      child: CustomEasyRefreshList<GetTbBaseMeasuresTaskListEntity>(
                        key: controller.globalKey,
                        dataLoad: controller.getTbBaseMeasuresTaskList,
                        itemBuilder: (item, index) {
                          return CustomTitleChartPage(
                            title: item.troubleshootContent ?? '',
                            child: Column(
                              children: [
                                _isYesSeason(item),
                                _isNoSeason(item),
                                AppGaps.vGap10,
                                _delAndEdit(item,controller),
                              ],
                            ),
                          ).click(() {
                            return controller.onItemOnClickDetail(item.id);
                          });
                        },
                      ),
                    )
                  ],
                ),
              ),
              Positioned(
                  right: 27,
                  bottom: 160,
                  child: SvgPicture.asset(
                    AppImages.icon_all_add_data,
                    width: 50.w,
                    height: 50.w,
                  ).clickOnce(controller.onClickAdd)),
            ],
          ),
          // endDrawer: CustomEndDrawer(
          //   centerContext: Column(
          //     children: [
          //       RegionalOrganizationTreePage(
          //         selectedEntity: controller.selectedEntity,
          //         onCallBack: (RiskTreeEntity entity) {
          //           controller.selectedEntity = entity;
          //         },
          //       ),
          //     ],
          //   ),
          //   reset: controller.reset,
          //   confirm: controller.confirm,
          // ),
        );
      },
    );
  }

  Widget _topTitle(String title, String icon) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        SvgPicture.asset(icon),
        AppGaps.hGap4,
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            color: const Color(0xFF5C6DFF),
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
      ],
    );
  }

  ///筛选按钮
  Widget _getFilter(HazardInvestigationTaskManagementController controller) {
    return SizedBox(
      width: 70.w,
      height: 38.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '筛选',
            style: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
          ),
          Icon(
            Icons.keyboard_arrow_down_rounded,
            size: 20.w,
            color: const Color(0xff495061),
          )
        ],
      ),
    ).click(controller.onFilterButtonPressed);
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 12.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0xFFFFFFFF),
        Color(0xFFFFFFFF),
      ])),
    );
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Text(
          value,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )
      ],
    );
  }

  Widget _topWidget(HazardInvestigationTaskManagementController controller) {
    return Container(
      height: 38.w,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8)),
      child: Row(
        children: [
          AppGaps.hGap10,
          Expanded(
            child: Text(
              controller.title ?? '',
              style: TextStyle(
                fontSize: 12.sp,
              ),
            ),
          ),
          _getFilter(controller),
        ],
      ),
    );
  }

  Widget _delAndEdit(GetTbBaseMeasuresTaskListEntity item, HazardInvestigationTaskManagementController controller) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: const Color(0x1A5C6DFF),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          _topTitle('编辑', AppImages.icon_double_edit).clickOnce(() {
            controller.onClickEdit(item.id);
          }),
          _divider(),
          _topTitle('删除', AppImages.icon_double_delete).clickOnce(() {
            Get.dialog<bool>(ExitTips(
              content: '是否确定删除？',
              func: (v) {
                if (v) {
                  controller.delTbBaseMeasuresTask(item.id);
                }
              },
            ));
          }),
        ],
      ),
    );
  }

  Widget _isYesSeason(GetTbBaseMeasuresTaskListEntity item) {
    return Visibility(
        visible: item.workKindLabel == '季节性排查' || item.workKindLabel == '重点时段及节假日前排查',
        child: Row(
          children: [
            _rowData(mainAxisAlignment: MainAxisAlignment.start, '${item.workKindLabel}  |  ', ''),
            Expanded(
                child: Row(
              children: item.workKindCycleChangeList != null
                  ? item.workKindCycleChangeList!
                      .map((e) => _rowData(mainAxisAlignment: MainAxisAlignment.start, '${e.checkCycle ?? '空'}${e.checkCycleUnitLabel ?? '空'} ', ''))
                      .toList()
                  : [],
            ))
          ],
        ));
  }

  Widget _isNoSeason(GetTbBaseMeasuresTaskListEntity item) {
    return Visibility(
        visible: !(item.workKindLabel == '季节性排查' || item.workKindLabel == '重点时段及节假日前排查'),
        child: _rowData('${item.workKindLabel}  |  ${item.checkCycle ?? '空'}${item.checkCycleUnitLabel ?? '空'}', ''));
  }
}
